#include "glwidget.h"

GLWidget::GLWidget (QWidget* parent)
	: QGLWidget (parent)
{
    rot = 0.0f;

	resize (sizeHint ());
}

GLWidget::~GLWidget ()
{
}

QSize GLWidget::minimumSizeHint () const
{
	return QSize (50, 50);
}

QSize GLWidget::sizeHint () const
{
	return QSize (800, 600);
}

void GLWidget::initializeGL ()
{
	glShadeModel(GL_SMOOTH);			// Enables Smooth Color Shading
	
    glClearColor(0.3f, 0.5f, 0.5f, 0.0f);		// This Will Clear The Background Color To Black
    glClearDepth(1.0f);				// Enables Clearing Of The Depth Buffer
	
	glEnable(GL_COLOR_MATERIAL);
    glEnable(GL_DEPTH_TEST);		        // Enables Depth Testing
	glDepthFunc(GL_LEQUAL);			        // The Type Of Depth Test To Do
	glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);			// Really Nice Perspective Calculations
	
    glEnable(GL_NORMALIZE);

    glMatrixMode(GL_MODELVIEW);

	glEnable(GL_LIGHTING);
    glEnable(GL_LIGHT0);                             // turn light 1 on.

    /* white ambient light at half intensity (rgba) */
    GLfloat LightAmbient[] = { 0.5f, 0.5f, 0.5f, 0.5f };
    /* super bright, full intensity diffuse light. */
    GLfloat LightDiffuse[] = { 1.0f, 1.0f, 1.0f, 0.2f };
    /* position of light (x, y, z, (position of light)) */
    GLfloat LightPosition[] = { 0.0f, 5.0f, -2.0f, 0.0f };

    // set up light number 1.
    glLightfv(GL_LIGHT0, GL_AMBIENT, LightAmbient);  // add lighting. (ambient)
    glLightfv(GL_LIGHT0, GL_DIFFUSE, LightDiffuse);  // add lighting. (diffuse).
    glLightfv(GL_LIGHT0, GL_POSITION,LightPosition); // set light position.

    Reine::creerListe();

    black_queen = new Reine(Reine::Blanc);
}

void GLWidget::paintGL ()
{

	glClear(GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT);	// Clear The Screen And The Depth Buffer

    glLoadIdentity();					// Reset The View
	glTranslatef(0.0f,-1.0f,-10.0f);				// Move Left And Into The Screen
	glRotatef(rot,0.0f,1.0f,1.0f);				// Rotate The Pyramid On It's Y Axis

	black_queen->dessiner();

    rot+=1.5f;					// Increase The Rotation

    update();
}

void GLWidget::resizeGL (int width, int height)
{
	if (height==0)				// Prevent A Divide By Zero If The Window Is Too Small
        height=1;

    glViewport(0, 0, width, height);		// Reset The Current Viewport And Perspective Transformation

    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();

    gluPerspective(45.0f,(GLfloat)width/(GLfloat)height,0.1f,100.0f);
    glMatrixMode(GL_MODELVIEW);
}

void GLWidget::mousePressEvent (QMouseEvent* event)
{
}

void GLWidget::mouseReleaseEvent (QMouseEvent* event)
{
}

void GLWidget::keyPressEvent (QKeyEvent* event)
{
}

void GLWidget::keyReleaseEvent (QKeyEvent* event)
{
}
